using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using TemplateWeb.Helpers;
using TemplateWeb.DAO;

namespace TemplateWeb.DTO
{
	/// <summary>
	/// Data transfer object which is associated with DanhMucCon table in database
	/// </summary>
	public class DanhMucConDTO
	{
		public int ID { get; set; }
		public int IDDanhMucChinh { get; set; }
		public string TenDanhMucCon { get; set; }

        public DanhMucChinhDTO DanhMucChinh { get; set; }
        public List<PhienDauGiaDTO> PhienDauGia { get; set; }

		public static DanhMucConDTO Translate(DataRow row)
		{
			var dto = new DanhMucConDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				IDDanhMucChinh = int.Parse(row["IDDanhMucChinh"].ToString()),
				TenDanhMucCon = row["TenDanhMucCon"].ToString(),
			};

            var dmch = new DanhMucChinhDTO
            {
                ID = int.Parse(row["ID"].ToString()),
                TenDanhMucChinh = row["TenDanhMucChinh"].ToString()
            };

            dto.DanhMucChinh = dmch;
            dto.PhienDauGia = new List<PhienDauGiaDTO>();
            // Select danh muc con
            var rpt = new PhienDauGiaDAO(WebConfiguration.DbConnectionString);
            var objDS = rpt.SelectByIDDanhMucCon(dto.ID);
            if (objDS != null && objDS.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in objDS.Tables[0].Rows)
                {
                    dto.PhienDauGia.Add(PhienDauGiaDTO.Translate(datarow));
                }
            }

			return dto;
		}
	}
}